package com.kingsoft.miot.bridge.controller;

import java.util.*;
import java.util.concurrent.CompletableFuture;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingsoft.miot.bridge.pojo.common.*;
import com.kingsoft.miot.bridge.pojo.device.Warning;
import com.kingsoft.miot.bridge.service.DeviceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kingsoft.miot.bridge.common.CommonResult;
import com.kingsoft.miot.bridge.controller.resp.DeviceInfoBrief;
import com.kingsoft.miot.bridge.utils.ValidationUtils;

/**
 * 设备 controller
 *
 * @author HUJIANQING
 */
@RestController
public class DeviceController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeviceController.class);

    @Autowired
    private DeviceService deviceService;


    @RequestMapping(value = "/data/create", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public CompletableFuture<String> createDevice(@RequestBody JSONObject params) {
        return CompletableFuture.supplyAsync(() -> {

            return CommonResult.jsonString();
        });
    }

    /**
     * 设备管理-根获取设备列表
     * 请求方式：POST
     *
     * @return
     */
    @RequestMapping(value = "/data/devices", method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    public String getDevices(@RequestBody JSONObject params) {
        LOGGER.info("设备管理-根获取设备列表->" + params);
        JSONObject resultJson = new JSONObject();
        resultJson.put("total_pages", 1);
        resultJson.put("total_count", 2);
        resultJson.put("current_page", 1);
        resultJson.put("size", 10);
        resultJson.put("devices_total_num", 500);
        resultJson.put("devices_online_num", 100);
        resultJson.put("operations_total_num", 2000);
        resultJson.put("active_devices_today", 123);

        // 设备数据
        DeviceInfoBrief deviceInfoBrief1 = new DeviceInfoBrief();
        DeviceInfoBrief deviceInfoBrief2 = new DeviceInfoBrief();
        DeviceInfoBrief deviceInfoBrief3 = new DeviceInfoBrief();
        deviceInfoBrief1.setId(223);
        deviceInfoBrief1.setName("格力掌握核心科技 ");
        deviceInfoBrief1.setType("空调");
        deviceInfoBrief1.setStatus(ValidationUtils.getOnlineStatus(0));
        deviceInfoBrief1.setLast_online_time(new Date().getTime());

        deviceInfoBrief2.setId(1234);
        deviceInfoBrief2.setName("格力掌握核心科技 ");
        deviceInfoBrief2.setType("电视");
        deviceInfoBrief2.setStatus(ValidationUtils.getOnlineStatus(0));
        deviceInfoBrief2.setLast_online_time(new Date().getTime());

        deviceInfoBrief3.setId(223);
        deviceInfoBrief3.setName("格力掌握核心科技 ");
        deviceInfoBrief3.setType("智能灯");
        deviceInfoBrief3.setStatus(ValidationUtils.getOnlineStatus(1));
        deviceInfoBrief3.setLast_online_time(new Date().getTime());

        List<DeviceInfoBrief> list = new ArrayList<>();
        list.add(deviceInfoBrief1);
        list.add(deviceInfoBrief2);
        list.add(deviceInfoBrief3);
        resultJson.put("item_list", list);

        return CommonResult.jsonString(resultJson);
    }

    /**
     * 房间管理-房间详情-设备-更新设备使用状态
     * 请求方式：POST
     *
     * @return
     */
    @RequestMapping(value = "/data/hotels/{id}/rooms/{room_id}/devices/{device_id}",
            produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public CompletableFuture<String> updateUseStatus(@RequestBody JSONObject params, @PathVariable int id,
                                                     @PathVariable int room_id, @PathVariable int device_id) {
        /**取出条件*/
        String useStatus = params.getString("use_status");
        LOGGER.info(useStatus + id + room_id + device_id);
        JSONObject resultJson = new JSONObject();
        resultJson.put("result", true);
        resultJson.put("code", 200);
        resultJson.put("message", "状态更新成功");
        return CompletableFuture.supplyAsync(() -> {
            return CommonResult.jsonString(resultJson);
        });
    }

    /**
     * 房间管理-房间详情-设备-消除报警
     * 请求方式：GET
     *
     * @return
     */
    @RequestMapping(value = "/data/hotels/{id}/rooms/{room_id}/devices/{device_id}/remove_warn",
            produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public CompletableFuture<String> removeWarn(@PathVariable int id, @PathVariable int room_id, @PathVariable int device_id) {
        LOGGER.info("结果是" + id + room_id + device_id);
        JSONObject resultJson = new JSONObject();
        resultJson.put("result", true);
        resultJson.put("code", 200);
        resultJson.put("message", "消除报警成功");
        return CompletableFuture.supplyAsync(() -> {
            return CommonResult.jsonString(resultJson);
        });
    }

    /**
     * 房间管理-房间详情-设备-获取房间设备
     *
     * @return
     */
    @RequestMapping(value = "/data/hotels/{id}/rooms/{room_id}/devices",
            produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public CompletableFuture<String> findDeviceByRoom(@PathVariable int id, @PathVariable int room_id) {
        LOGGER.info("结果是" + id + room_id);
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("id", 1);
        jsonObject1.put("name", "空调");
        jsonObject1.put("location", "卧室");
        jsonObject1.put("icon", "http://placeimg.com/63/78/any");
        jsonObject1.put("device_status", ValidationUtils.getOnlineStatus(0));
        jsonObject1.put("use_status", ValidationUtils.getDeviceStatus(0));
        jsonObject1.put("info", "制冷 26度");

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("id", 2);
        jsonObject2.put("name", "窗帘");
        jsonObject2.put("location", "卧室");
        jsonObject2.put("icon", "http://placeimg.com/80/45/any");
        jsonObject2.put("device_status", ValidationUtils.getOnlineStatus(1));
        jsonObject2.put("use_status", ValidationUtils.getDeviceStatus(1));
        jsonObject2.put("info", "");

        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("id", 3);
        jsonObject3.put("name", "加湿器");
        jsonObject3.put("location", "卧室");
        jsonObject3.put("icon", "http://placeimg.com/63/78/any");
        jsonObject3.put("device_status", ValidationUtils.getOnlineStatus(2));
        jsonObject3.put("use_status", ValidationUtils.getDeviceStatus(0));
        jsonObject3.put("info", "制冷 26度");
        jsonArray.add(jsonObject1);
        jsonArray.add(jsonObject2);
        jsonArray.add(jsonObject3);
        return CompletableFuture.supplyAsync(() ->
        {
            return CommonResult.jsonString(jsonArray);
        });
    }

    /**
     * 房间管理-房间详情-设备-解绑用户
     *
     * @return
     */
    @RequestMapping(value = "/data/hotels/{id}/rooms/{room_id}/unbound",
            produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public CompletableFuture<String> unboundUser(@PathVariable int id, @PathVariable int room_id) {
        LOGGER.info("结果是" + id + room_id);
        JSONObject resultJson = new JSONObject();
        resultJson.put("result", true);
        resultJson.put("code", 200);
        resultJson.put("message", "解绑用户成功");
        return CompletableFuture.supplyAsync(() -> {
            return CommonResult.jsonString(resultJson);
        });
    }

    /**
     * 设备管理-设备详情-根据设备id获取设备列表
     * 请求方式：POST
     *
     * @return
     */
    @RequestMapping(value = "/data/devices/{device_id}",
            produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public CompletableFuture<String> findDeviceByDeviceId(@RequestBody JSONObject params, @PathVariable int device_id) {
        LOGGER.info("设备管理-设备详情-根据设备id获取设备列表->" + params + device_id);
        return CompletableFuture.supplyAsync(() -> {
            JSONObject resultJson = new JSONObject();
            resultJson.put("total_pages", 7);
            resultJson.put("total_count", 20);
            resultJson.put("current_page", 2);
            resultJson.put("size", 3);
            resultJson.put("id", 1);
            resultJson.put("type", "空调");
            resultJson.put("name", "123空调");
            resultJson.put("hotel_name", "全季上海南京路店");
            resultJson.put("status", ValidationUtils.getHouseStatus(0));
            resultJson.put("last_online_time", 1234590123);
            JSONArray info = new JSONArray();
            JSONArray dataArray = new JSONArray();
            JSONObject timeAndData = new JSONObject();
            timeAndData.put("time", "1234567890123");
            timeAndData.put("status", ValidationUtils.getHouseStatus(0));
            JSONObject timeAndData1 = new JSONObject();
            timeAndData1.put("time", "1234567890124");
            timeAndData1.put("status", ValidationUtils.getHouseStatus(1));
            JSONObject timeAndData2 = new JSONObject();
            timeAndData2.put("time", "1234567890125");
            timeAndData2.put("status", ValidationUtils.getHouseStatus(0));
            dataArray.add(timeAndData);
            dataArray.add(timeAndData1);
            dataArray.add(timeAndData2);
            JSONObject timeDate = new JSONObject();
            timeDate.put("time", "1234567890123");
            timeDate.put("data", dataArray);
            info.add(timeDate);
            resultJson.put("info", info);
            return CommonResult.jsonString(resultJson);
        });
    }

    /**
     * 设备管理-活跃统计-获取设备统计活跃和控制数据
     * 请求方式：GET
     *
     * @return
     */
    @RequestMapping(value = "/data/devices/statistics",
            produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public CompletableFuture<String> statistics() {
        JSONArray resultArray = new JSONArray();
        JSONObject resultJson1 = new JSONObject();
        resultJson1.put("name", "今日活跃");
        resultJson1.put("data", 124);
        JSONObject resultJson2 = new JSONObject();
        resultJson2.put("name", "昨日活跃");
        resultJson2.put("data", 333);
        JSONObject resultJson3 = new JSONObject();
        resultJson3.put("name", "近七日活跃");
        resultJson3.put("data", 3445);
        JSONObject resultJson4 = new JSONObject();
        resultJson4.put("name", "近30日活跃");
        resultJson4.put("data", 4959);
        JSONObject resultJson5 = new JSONObject();
        resultJson5.put("name", "今日控制次数");
        resultJson5.put("data", 124);
        JSONObject resultJson6 = new JSONObject();
        resultJson6.put("name", "昨日控制次数");
        resultJson6.put("data", 333);
        JSONObject resultJson7 = new JSONObject();
        resultJson7.put("name", "近七日控制次数");
        resultJson7.put("data", 3445);
        JSONObject resultJson8 = new JSONObject();
        resultJson8.put("name", "近30日控制次数");
        resultJson8.put("data", 4959);
        resultArray.add(resultJson1);
        resultArray.add(resultJson2);
        resultArray.add(resultJson3);
        resultArray.add(resultJson4);
        resultArray.add(resultJson5);
        resultArray.add(resultJson6);
        resultArray.add(resultJson7);
        resultArray.add(resultJson8);
        return CompletableFuture.supplyAsync(() -> {
            return CommonResult.jsonString(resultArray);
        });
    }

    /**
     * 设备管理-活跃统计-获取设备统计类别列表
     *
     * @return
     */
    @RequestMapping(value = "/data/devices/type",
            produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public CompletableFuture<String> findTypeList() {
        JSONArray resultArray = new JSONArray();
        JSONObject resultJson1 = new JSONObject();
        resultJson1.put("name", "活跃");
        resultJson1.put("data", 0);
        JSONObject resultJson2 = new JSONObject();
        resultJson2.put("name", "控制");
        resultJson2.put("data", 1);
        JSONObject resultJson3 = new JSONObject();
        resultJson3.put("name", "活跃+控制");
        resultJson3.put("data", 2);
        resultArray.add(resultJson1);
        resultArray.add(resultJson2);
        resultArray.add(resultJson3);
        return CompletableFuture.supplyAsync(() -> {
            return CommonResult.jsonString(resultArray);
        });
    }


    /**
     * 设备管理-活跃统计-获取活跃和控制趋势统计
     * 请求方式：POST
     *
     * @return
     */
    @RequestMapping(value = "/data/devices/statistics",
            produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public CompletableFuture<String> findStatistics(@RequestBody JSONObject params) {
        LOGGER.info("设备管理-活跃统计-获取活跃和控制趋势统计->" + params);
        return CompletableFuture.supplyAsync(() -> {
            JSONObject resultJson = new JSONObject();
            JSONArray seriesNames = new JSONArray();
            seriesNames.add("近30日个品类活跃和控制趋势");
            JSONArray dataArray = new JSONArray();
            for (int i = 0; i < 5; i++) {
                StatisticsData statisticsData = new StatisticsData();
                statisticsData.setName("aaaaa");
                statisticsData.setValue(15551.40263680297);
                StatisticsData statisticsData1 = new StatisticsData();
                statisticsData1.setName("bbbbb");
                statisticsData1.setValue(5577.887235112961);
                List list = new ArrayList();
                list.add(statisticsData);
                list.add(statisticsData1);
                Tooltips tooltips = new Tooltips();
                tooltips.setData(list);
                tooltips.setUnit("单位");
                StatisticsBean statisticsBean = new StatisticsBean();
                statisticsBean.setValue(99.82998594154479);
                statisticsBean.setTooltips(tooltips);
                dataArray.add(statisticsBean);
            }
            JSONObject series = new JSONObject();
            series.put("data", dataArray);
            series.put("seriesName", "近30日个品类活跃和控制趋势");

            resultJson.put("seriesNames", seriesNames);
            resultJson.put("series", series);
            resultJson.put("unit", "y轴单位");
            resultJson.put("xAxis", new int[]{1515542400,
                    1515628800,
                    1515715200,
                    1515801600,
                    1515888000,
                    1515974400,
                    1516060800});

            return CommonResult.jsonString(resultJson);
        });
    }


    /**
     * 设备管理-报警中心-获取设备管理报警列表
     * 请求方式：POST
     *
     * @return
     */
    @RequestMapping(value = "/data/devices/warning_list",
            produces = {"application/json;charset=UTF-8"})
    public CompletableFuture<String> findWarningList(@RequestBody JSONObject params) {
        LOGGER.info("设备管理-报警中心-获取设备管理报警列表->" + params);
        return CompletableFuture.supplyAsync(() -> {
            JSONObject resultJson = new JSONObject();
            resultJson.put("total_pages", 6);
            resultJson.put("total_count", 58);
            resultJson.put("current_page", 1);
            resultJson.put("size", 10);
            Warning warning = new Warning();
            warning.setId(1);
            warning.setName("123空调");
            warning.setHotel_name("全季上海南京路店");
            warning.setRoom_num(201);
            warning.setWarning_time(new Date());
            warning.setInfo("掉线");
            Warning warning1 = new Warning();
            warning1.setId(2);
            warning1.setName("234空调");
            warning1.setHotel_name("全季上海南京路店");
            warning1.setRoom_num(208);
            warning1.setWarning_time(new Date());
            warning1.setInfo("掉线");
            JSONArray list = new JSONArray();
            list.add(warning);
            list.add(warning1);
            resultJson.put("list", list);
            return CommonResult.jsonString(resultJson);
        });
    }
}
